//
//  Alert.m
//  OCBase
//
//  Created by 侯伟 on 2019/4/16.
//  Copyright © 2019年 侯伟. All rights reserved.
//

#import "Alert.h"
#import "UIViewController+Top.h"

@implementation Alert


+ (UIAlertController*)systemAlert:(NSString*)title message:(NSString*)message completion:(VoidBlock)completion{
    UIAlertController* alertController = [UIAlertController alertControllerWithTitle:title message:message preferredStyle:(UIAlertControllerStyleAlert)];
    UIAlertAction* action = [UIAlertAction actionWithTitle:@"我知道了" style:(UIAlertActionStyleDefault) handler:^(UIAlertAction * _Nonnull action) {
        if(completion){
            completion();
        }
    }];
    [alertController addAction:action];
    
    [[UIViewController topViewController] presentViewController:alertController animated:YES completion:NULL];
    return alertController;
}
/// 弹框
/// @param title 标题
/// @param message 消息
/// @param completion 回调
+ (RSAlartViewController*)alert:(NSString*)title message:(NSString*)message completion:(AlertBlock)completion{
    
    RSAlartViewController* alert = [RSAlartViewController new];
    alert.titleStr = title;
    alert.messageStr = message;
    alert.alertBlock = completion;
    [alert presentOverViewController:[UIViewController topViewController]];
    return alert;
}

/// 选择弹框
/// @param title 标题
/// @param message 消息
/// @param completion 回调
+ (RSConfirmViewController*)confirm:(NSString*)title message:(NSString*)message completion:(ConfirmBlock)completion{
    
    RSConfirmViewController* confirm = [RSConfirmViewController new];
    confirm.titleStr = title;
    confirm.messageStr = message;
    confirm.confirmBlock = completion;
    
    [confirm presentOverViewController:[UIViewController topViewController]];
    return confirm;
}


+ (RSConfirmViewController*)confirmChange:(NSString* _Nullable)title message:(NSString* __nullable)message confirmTitle:(NSString* __nullable)confirmTitle  cancelTitle:(NSString* __nullable)cancelTitle completion:(ConfirmBlock)completion{
    
    RSConfirmViewController* confirm = [RSConfirmViewController new];
    confirm.titleStr = title;
    confirm.messageStr = message;
    confirm.confirmTitle = confirmTitle;
    confirm.cancelTitle = cancelTitle;
    confirm.confirmBlock = completion;
    
    [confirm presentOverViewController:[UIViewController topViewController]];
    return confirm;
    
}


/// 输入出弹出框
/// @param title 标题
/// @param text 默认文本
/// @param placeholder 占位文本
/// @param completion 回调
+(RSInputViewController*)prompt:(NSString*)title text:(NSString*)text placeholder:(NSString*)placeholder completion:(InputBlock)completion{
    
    RSInputViewController* input = [RSInputViewController new];
    input.placeholder = placeholder;
    input.titleStr = title;
    input.text = text;
    input.keyboardType = UIKeyboardTypeDefault;
    input.inputBlock = completion;
    [input presentOverViewController:[UIViewController topViewController]];
    
    return input;
}

/// 输入出弹出框
/// @param title 标题
/// @param text 默认文本
/// @param placeholder 占位文本
/// @param completion 回调
+(UIAlertController*)systemPrompt:(NSString*)title text:(NSString*)text placeholder:(NSString*)placeholder completion:(InputBlock)completion{
    
    UIAlertController* alertController = [UIAlertController alertControllerWithTitle:title message:NULL preferredStyle:(UIAlertControllerStyleAlert)];
    
    [alertController addTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textField) {
        textField.text = text;
        textField.placeholder = placeholder;
        textField.keyboardType = UIKeyboardTypeDecimalPad;
        textField.text = text;
    }];
    UIAlertAction* sure = [UIAlertAction actionWithTitle:@"确定" style:(UIAlertActionStyleDefault) handler:^(UIAlertAction * _Nonnull action) {
        
        NSString* text = alertController.textFields.firstObject.text;
        
        if(completion){
            completion(text);
        }
    }];
    UIAlertAction* cancel = [UIAlertAction actionWithTitle:@"取消" style:(UIAlertActionStyleDefault) handler:^(UIAlertAction * _Nonnull action) {

    }];
    [alertController addAction:cancel];
    [alertController addAction:sure];
    
    [[UIViewController topViewController] presentViewController:alertController animated:YES completion:NULL];
    return alertController;
}

@end
